<?php

namespace app\modules\Product\controllers;

use Yii;
use app\modules\Product\models\ProductFlight;
use app\modules\Product\models\ProductFlightSearch;
use app\modules\Product\models\Product;
use app\modules\Product\models\ProductGroup;
use app\modules\Product\models\ProductVisit;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;


class ProductExtraController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    public function actionIndex($product_id)
    {   
        $product = Product::findOne($product_id);

        $flight = ProductFlight::find()->where(['product_id'=>$product_id])->all();
        $group = ProductGroup::find()->where(['product_id'=>$product_id])->all();
        $visit = ProductVisit::find()->where(['product_id'=>$product_id])->all();

        return $this->render('index', [
            'product' => $product,
            'flight' => $flight,
            'group'  => $group,    
            'visit' => $visit,
        ]);
    }  

    /**
     * 创建机票、地接酒店、游览(岸上游)信息
     * 
     */
    public function actionCreate($product_id)
    {
        $product = Product::findOne($product_id);
        $flight = new ProductFlight();
        $group = new ProductGroup();
        $visit = new ProductVisit();

        $data = yii::$app->request->post();

        if(!empty($data)){
            $flight_data = $data['ProductFlight'];
            foreach($flight_data['name'] as $i=>$v){
                if(!empty($flight_data['name'][$i]) || !empty($flight_data['num'][$i]) || !empty($flight_data['price'][$i]) || !empty($flight_data['remark'][$i])){
                    $_flight = clone $flight;
                    $_flight->product_id = $data['product_id'];
                    $_flight->name = $flight_data['name'][$i];
                    $_flight->num = $flight_data['num'][$i];
                    $_flight->price = $flight_data['price'][$i];
                    $_flight->remark = $flight_data['remark'][$i];
                    $_flight->save(); 
                }
            }
            $group_data = $data['ProductGroup'];
            foreach($group_data['name'] as $i=>$v){
                if(!empty($group_data['name'][$i]) || !empty($group_data['num'][$i]) || !empty($group_data['price'][$i]) || !empty($group_data['remark'][$i])){
                    $_group = clone $group;
                    $_group->product_id = $data['product_id'];
                    $_group->name = $group_data['name'][$i];
                    $_group->num = $group_data['num'][$i];
                    $_group->price = $group_data['price'][$i];
                    $_group->remark = $group_data['remark'][$i];
                    $_group->save(); 
                }
            }
            $visit_data = $data['ProductVisit'];
            foreach($visit_data['name'] as $i=>$v){
                if(!empty($visit_data['name'][$i]) || !empty($visit_data['num'][$i]) || !empty($visit_data['price'][$i]) || !empty($visit_data['remark'][$i])){
                    $_visit = clone $visit;
                    $_visit->product_id = $data['product_id'];
                    $_visit->name = $visit_data['name'][$i];
                    $_visit->num = $visit_data['num'][$i];
                    $_visit->price = $visit_data['price'][$i];
                    $_visit->remark = $visit_data['remark'][$i];
                    $_visit->save(); 
                }
            }
            return $this->redirect(['index', 'product_id' => $product->id]);
        }
        
        return $this->render('create', [
                'flight' => $flight,
                'product' => $product,
                'visit' => $visit,
                'group' => $group,
            ]);
    }

    
    public function actionFlightView($id)
    {
        $model = ProductFlight::findOne($id);
        $product = Product::findOne($model->product_id);
        return $this->render('flight-view', [
            'model' => $model,
            'product' =>$product,
        ]);

    }

    public function actionFlightUpdate($id)
    {
        $model = ProductFlight::findOne($id);
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['flight-view', 'id' => $model->id]);
        } else {
            return $this->render('flight-update', [
                'model' => $model,
            ]);
        }
    }

    public  function actionFlightUpnum($flight_id,$num,$action)
    {
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $model = ProductFlight::findOne($flight_id);
        if($model === null || $num == ''){
            return ['code'=>0,'num'=>'']; 
        }
        $transaction = Yii::$app->db->beginTransaction();
        try{
            if($action == 0){
                $model->num = intval($model->num)-intval($num);
            }elseif ($action == 1) {
                $model->num = intval($model->num)+intval($num);
            }
            
            if($model->num < 0 ){
                return ['code'=>0,'num'=>'']; 
            }
            $res = $model->save();
            if(!$res){
                return ['code'=>0,'num'=>'']; 
            }
            $transaction->commit(); 
            return ['code'=>1,'num'=>$model->num]; 
        }catch (\Exception $e){
            $transaction->rollBack();
            return ['code'=>0,'num'=>'']; 
        }

    }


     public function actionGroupView($id)
    {
        $model = ProductGroup::findOne($id);
        $product = Product::findOne($model->product_id);
        return $this->render('group-view', [
            'model' => $model,
            'product' =>$product,
        ]);

    }

    public function actionGroupUpdate($id)
    {
        $model = ProductGroup::findOne($id);
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['group-view', 'id' => $model->id]);
        } else {
            return $this->render('group-update', [
                'model' => $model,
            ]);
        }
    }

    public  function actionGroupUpnum($group_id,$num,$action)
    {
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $model = ProductGroup::findOne($group_id);
        if($model === null || $num == ''){
            return ['code'=>0,'num'=>'']; 
        }
        $transaction = Yii::$app->db->beginTransaction();
        try{
            if($action == 0){
                $model->num = intval($model->num)-intval($num);
            }elseif ($action == 1) {
                $model->num = intval($model->num)+intval($num);
            }
            
            if($model->num < 0 ){
                return ['code'=>0,'num'=>'']; 
            }
            $res = $model->save();
            if(!$res){
                return ['code'=>0,'num'=>'']; 
            }
            $transaction->commit(); 
            return ['code'=>1,'num'=>$model->num]; 
        }catch (\Exception $e){
            $transaction->rollBack();
            return ['code'=>0,'num'=>'']; 
        }

    }


     public function actionVisitView($id)
    {
        $model = ProductVisit::findOne($id);
        $product = Product::findOne($model->product_id);
        return $this->render('visit-view', [
            'model' => $model,
            'product' =>$product,
        ]);

    }

    public function actionVisitUpdate($id)
    {
        $model = ProductVisit::findOne($id);
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['visit-view', 'id' => $model->id]);
        } else {
            return $this->render('visit-update', [
                'model' => $model,
            ]);
        }
    }

    public  function actionVisitUpnum($visit_id,$num,$action)
    {
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $model = ProductVisit::findOne($visit_id);
        if($model === null || $num == ''){
            return ['code'=>0,'num'=>'']; 
        }
        $transaction = Yii::$app->db->beginTransaction();
        try{
            if($action == 0){
                $model->num = intval($model->num)-intval($num);
            }elseif ($action == 1) {
                $model->num = intval($model->num)+intval($num);
            }
            
            if($model->num < 0 ){
                return ['code'=>0,'num'=>'']; 
            }
            $res = $model->save();
            if(!$res){
                return ['code'=>0,'num'=>'']; 
            }
            $transaction->commit(); 
            return ['code'=>1,'num'=>$model->num]; 
        }catch (\Exception $e){
            $transaction->rollBack();
            return ['code'=>0,'num'=>'']; 
        }

    }

    
}
